/**
 * 
 */
package cn.gov.msa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.collection.LdapService;
import cn.gov.msa.collection.SharedLibraryService;
import cn.gov.msa.dao.CommonOpinionMapper;
import cn.gov.msa.dao.UserDetailMapper;
import cn.gov.msa.model.CommonOpinion;
import cn.gov.msa.model.Dictionary;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.model.Role;
import cn.gov.msa.model.User;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.model.UserRole;
import cn.gov.msa.service.CommonOpinionService;
import cn.gov.msa.service.RoleService;
import cn.gov.msa.service.UserDetailService;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**   
* @Title: UserController.java 
* @Package cn.gov.msa.controller 
* @Description: TODO(与其它系统集成的Controller) 
* @author 何宇江  
* @date 2018年4月5日 下午8:40:32 
*/

@RestController
@RequestMapping(value = "/sys", produces="text/plain;charset=UTF-8")
public class SysController {

	
	@RequestMapping(value = "/ship")
	public String shipList(@RequestBody JSONObject params) throws Exception {
		String result = "";
		
		
		
		result = SharedLibraryService.queryShip(params);
		
		return result;
	}
	
	

	LdapService ldapService = new LdapService();

	@RequestMapping(value = "/userquery")
	public String userQuery(@RequestBody JSONObject params) throws Exception {
		String result = "";
		System.err.println(params);
		
		if(!"".equals(params.getString("username")) || !"".equals(params.getString("orgcode"))){
			result = LdapService.queryUser(params);
			System.err.println(result);
		}
		else{
			result= JSONObject.fromObject("{\"total\":0, \"rows\":[]}").toString();
		}
		
		
		return result;
	}

	@Autowired
	private CommonOpinionMapper commonOpinionMapper;
	
	@RequestMapping(value = "/addcommonopinion", method=RequestMethod.POST)
	public String addcCommonOpinion(@RequestBody JSONObject param, HttpSession session) throws Exception {
		JSONObject json = new JSONObject();
		String result = "0";
		String info = InfoObjUtil.Error_save_no;
		
		try{
			String userid = session.getAttribute("userid").toString();
//			String userid = "210211197302115817";
			
			String opinion = param.optString("opinion");
			
			CommonOpinion record = new CommonOpinion();
			record.setUserid(userid);
			record.setOpinion(opinion);

			int i = commonOpinionMapper.insert(record);
		
			if(i > 0){
				result = "1";
				info = InfoObjUtil.Success_save_OK;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			result = "2";
			info = InfoObjUtil.Exption_msg;
		}
		
		json.put("result", result);	
		json.put("info", info);			
				
		return json.toString();
	}
	

	@RequestMapping(value = "/findcommonopinion", method=RequestMethod.GET)
	public String findCommonOpinion(HttpSession session) throws Exception {
		JSONObject json = new JSONObject();
		String result = "0";
		String info = InfoObjUtil.Error_NODATA;
		
		try{
			String userid = session.getAttribute("userid").toString();
//			String userid = "210211197302115817";
			
			CommonOpinion record = new CommonOpinion();
			record.setUserid(userid);

			List<CommonOpinion> commonOpinionList = commonOpinionMapper.selectData(record);
		
			if(null != commonOpinionList && commonOpinionList.size() > 0){
				result = "1";
				info = InfoObjUtil.Success_select_OK;
				
				json.put("datas", commonOpinionList);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			result = "2";
			info = InfoObjUtil.Exption_msg;
		}
		
		json.put("result", result);	
		json.put("info", info);			
				
		return json.toString();
	}
	
	@Autowired 
	CommonOpinionService commonOpinionService;

	@RequestMapping(value = "/opinionlist")
	public String opinionList(@RequestBody JSONObject params, HttpSession session) throws Exception {
		String result = "";
		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		

		String userid = session.getAttribute("userid").toString();
//		String userid = "210211197302115817";

		CommonOpinion record = (CommonOpinion)JSONObject.toBean(params, CommonOpinion.class);
		record.setUserid(userid);
		
		List<CommonOpinion> commonOpinionList = commonOpinionService.selectCommonOpinion(pageNum, pageSize, record);		

	    //获取分页信息
        PageInfo page = new PageInfo(commonOpinionList);
        

		JSONArray ja = JSONArray.fromObject(commonOpinionList);
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		
		
		result = jsonResult.toString();
		return result;
	}
	


	@RequestMapping(value = "/savecommonopinion")
	public String savCcommonOpinion(@RequestBody JSONObject param, HttpSession session) throws Exception{	
		int result = 0;
		String info = "";
	    JSONObject jsonResult = new JSONObject(); 
		
	    try{

			String userid = session.getAttribute("userid").toString();
//			String userid = "210211197302115817";
			
			//得到常用意见的model数据
			CommonOpinion commonOpinion = (CommonOpinion)JSONObject.toBean(param, CommonOpinion.class);
			commonOpinion.setUserid(userid);

			int i = commonOpinionMapper.insert(commonOpinion);
	
		    if(i > 0){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	
	
	@RequestMapping(value = "/updatecommonopinion")
	public String updateCommonOpinion(@RequestBody JSONObject param, HttpSession session) throws Exception{	
		int result = 0;
		String info = "";
	    JSONObject jsonResult = new JSONObject(); 
		
	    try{
			//得到常用意见的model数据
			CommonOpinion commonOpinion = (CommonOpinion)JSONObject.toBean(param, CommonOpinion.class);

			int i = commonOpinionMapper.updateByPrimaryKeySelective(commonOpinion);
	
		    if(i > 0){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}

	@RequestMapping(value = "/getcommonopinioninfo")
	public String getCommonOpinionInfo(String commonopinionid) throws Exception {
		int result = 0;
		String info = "";
		
		JSONObject joparam = new JSONObject();
		
		try{

			CommonOpinion commonOpinion = commonOpinionMapper.selectByPrimaryKey(commonopinionid);	
			
			result = commonOpinion != null ? 1 : 0;
			info = commonOpinion != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			joparam = JSONObject.fromObject(commonOpinion);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", joparam);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/deletecommonopinion")
	public String deleteCommonOpinion(String commonopinionid) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{
			
			int delete = commonOpinionMapper.deleteByPrimaryKey(commonopinionid);		
			
			result = delete > 0 ? 1 : 0;
			info = delete > 0 ? InfoObjUtil.Success_delete_OK : InfoObjUtil.Error_delete_no;
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/batchdeletecommonopinion")
	public String batchDeleteCommonOpinion(String commonopinionids) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{			
			String commonopinionidResult = "";
			
			for(int i = 0 ; i < commonopinionids.split(",").length; i++){	
				String commonopinionid = commonopinionids.split(",")[i];
				int delete = commonOpinionMapper.deleteByPrimaryKey(commonopinionid);
				
				if(delete > 0)
					commonopinionidResult += commonopinionid + ",";
			}
			
			commonopinionidResult = commonopinionidResult.substring(0, commonopinionidResult.length() - 1);

			JSONObject param = new JSONObject();
			param.put("commonopinionids", commonopinionidResult);
			japaram.add(param);
			
			result = commonopinionidResult.split(",").length > 0 ? 1 : 0;
			info = commonopinionidResult.split(",").length > 0 ? "批量删除成功！" : "批量删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
			e.printStackTrace();
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);		
		
		return jsonResult.toString();
	}
}
